package com.szosen.device.jianhuyi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.szosen.application.MyApplication;
import com.szosen.database.DBService;
import com.szosen.database.xueya;
import com.szosen.database.xueyang;
import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.jkzj.util.Contants;
import com.szosen.util.TimeHelper;
import com.szosen.util.ToastTool;
import com.zj.btsdk.BluetoothService;

import org.json.JSONException;

import java.util.Timer;
import java.util.TimerTask;

@SuppressLint({"InlinedApi", "ResourceAsColor"})
public class JianHuYiActivity extends BaseActivity implements OnClickListener {

    private static String deviceName = "监护仪";// 设备名称
    public static final int UPLOAD_RESULT_OK = 2047;
    public static final int UPLOAD_RESULT_ERROR = 0x10040;
    public static final int OFFLINE_ERROR = 0x10043;
    public static final int OFFLINE_OK = 0x10042;
    public static final int OFFLINE_RESULT = 0x10041;
    private static EditText HighBpEditText = null;// 高压文本框
    private static EditText lowBpEditText = null;// 低压文本框
    private static EditText pulseEditText = null;// 脉率
    private static EditText boEditText = null;// 血氧
    private static EditText tempEditText = null;// 体温
    private static Button measureBpButton = null;// 测试测量血压按钮
    private static Button stableBoButton = null;// 测试测量血氧按钮
    private static final int REQUEST_ENABLE_BT = 2;
    private static Button measureTempButton = null;// 测量心电按钮
    private static Button uploadButton;// 上次数据按钮
    private static boolean boStable = false;// 血氧恒定标记
    private static boolean tempStable = false;// 体温恒定标记
    private static Button findButton = null;// 查找设备按钮
    private static TextView connect_status = null;// 蓝牙连接状态
    private static ImageView hBpImageView = null;// 收缩压前面图标
    private static ImageView lBpImageView = null;// 舒张压前面图标
    private static ImageView pulseImageView = null;// 脉率前面图标
    private static ImageView boImageView = null;// 血氧前面图标
    private static ImageView tempImageView = null;// 体温前面图标
    private static final int REQUEST_CONNECT_DEVICE = 1; // 获取设备消息
    private Context context;
    String results = null;
    String btName = "一体监护仪";
    // String btMac = "一体监护仪";
    String btMac = "10";
    private TextView tv_showBoData;
    private static final String ACTION_USB_PERMISSION = MyApplication.ACTION_USB_PERMISSION;
    protected final Object ThreadLock = new Object();
    public static final int READER_DATA = 3;
    public static final int DISCONNT = 2;
    public static final String TAG = "com.wch.wchusbdriver";
    private UsbManager myManager;
    private BeneCheckDeviceHB beneCheckDevice;
    int n = 5;
    RadioButton btn_chengren, btn_ertong, btn_daby, btn_left, btn_right;
    com.zj.btsdk.BluetoothService mService = null;
    BluetoothDevice con_dev = null;
    LinearLayout measure_ll_xueya_mailv, measure_ll_xueyang, measure_ll_tiwen;
    Button btn_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.measure_on_jianhuyi);
        // 初始化控件
        findId();
        Intent intent = getIntent();
        flag = intent.getStringExtra("fromxueya");
        if ("xueya".equals(flag)) {
            uploadButton.setText("确定");
        }
        myManager = (UsbManager) getSystemService(Context.USB_SERVICE);
        mTime = TimeHelper.getCurrentTime();
    }

    public void finishActivity(View view) {
        onBackPressed();
        System.out.println("返回键点击了");
    }

    private String mTime;
    /**
     * 健康体检的主键的time用于创建数据库。
     */
    private String mjktjTime = TimeHelper.getMainTimeKey();

    /***
     * 上传血压\血氧\体温
     *
     * @throws JSONException
     */
    protected void upload() throws JSONException {
        String dbp = lowBpEditText.getText().toString().trim();// 舒张压
        if (dbp.length() > 0) {// 舒张压有数据，可以上传血压值
            xueya uploadBean = new xueya();
            uploadBean.setIdCard(BaseActivity.getIdCard());
            uploadBean.setTime(mTime);
            if (btn_chengren.isChecked()) {
                uploadBean.setPosition("成人");
            } else if (btn_ertong.isChecked()) {
                uploadBean.setPosition("儿童");

            } else if (btn_daby.isChecked()) {
                uploadBean.setPosition("婴儿");
            }
            if (btn_left.isChecked()) {
                uploadBean.setHand("左");
            } else if (btn_right.isChecked()) {
                uploadBean.setHand("右");
            }
            uploadBean.setDbp(lowBpEditText.getText().toString());
            uploadBean.setSbp(HighBpEditText.getText().toString());
            uploadBean.setPulse(pulseEditText.getText().toString());
            boolean b = DBService.add(uploadBean);
            if (b) {
                ToastTool.showLong(context, "数据保存成功");
            } else {
                ToastTool.showLong(context, "数据保存失败");
            }
            return;
        }

        // tempEditText.setText("38");
        // String temp = tempEditText.getText().toString();
        // if (temp.length() > 0) {// 体温有数据
        // }
        // boEditText.setText("98");
        String temp_bo = tv_showBoData.getText().toString().trim();
        if (!"--".equals(temp_bo)) {
            boEditText.setText(temp_bo);
        } else {
            return;
        }
        String bo = boEditText.getText().toString();
        if (bo.length() > 0) {// 血氧有数据
            xueyang xueyangBean = new xueyang();
            xueyangBean.setTime(mTime);
            xueyangBean.setIdCard(BaseActivity.getIdCard());
            xueyangBean.setBp(boEditText.getText().toString());
            boolean b = DBService.add(xueyangBean);
            if (b) {
                is_showtextShan = false;
                ToastTool.showLong(context, "数据保存成功");
            } else {
                ToastTool.showLong(context, "数据保存失败");
            }
            if (timer != null) {
                timer.cancel();
                if (task != null) {
                    task.cancel();
                }
            }
            if (beneCheckDevice.isConnected()) {
                beneCheckDevice.CloseDevice();
            }
        }
    }

    private void findId() {
        context = this;
        tv_showBoData = (TextView) findViewById(R.id.measure_jianhuyi_tv_bo_showData);
        HighBpEditText = (EditText) findViewById(R.id.hp);
        lowBpEditText = (EditText) findViewById(R.id.lp);
        pulseEditText = (EditText) findViewById(R.id.mb);
        boEditText = (EditText) findViewById(R.id.bo);
        tempEditText = (EditText) findViewById(R.id.temp);
        measureBpButton = (Button) findViewById(R.id.start_stop_bp_btn);
        stableBoButton = (Button) findViewById(R.id.bo_stable_button);
        measureTempButton = (Button) findViewById(R.id.start_stop_ecg_btn);
        findButton = (Button) findViewById(R.id.find_device);
        uploadButton = (Button) findViewById(R.id.upload_button);
        connect_status = (TextView) findViewById(R.id.connect_status);
        connect_status.setTextColor(Color.BLUE);
        hBpImageView = (ImageView) findViewById(R.id.hp_image);
        lBpImageView = (ImageView) findViewById(R.id.lp_image);
        pulseImageView = (ImageView) findViewById(R.id.pulse_image);
        boImageView = (ImageView) findViewById(R.id.bo_image);
        tempImageView = (ImageView) findViewById(R.id.temp_image);
        btn_chengren = (RadioButton) findViewById(R.id.btn_chengren);
        btn_ertong = (RadioButton) findViewById(R.id.btn_ertong);
        btn_daby = (RadioButton) findViewById(R.id.btn_baby);
        btn_left = (RadioButton) findViewById(R.id.btn_left);
        btn_right = (RadioButton) findViewById(R.id.btn_right);
        btn_left.setChecked(true);
        btn_chengren.setChecked(true);
        measureBpButton.setText("血压测量");
        stableBoButton.setText("重新测量");
        measureTempButton.setText("体温测量");
        findButton.setOnClickListener(this);
        measureBpButton.setOnClickListener(this);
        stableBoButton.setOnClickListener(this);
        measureTempButton.setOnClickListener(this);
        uploadButton.setOnClickListener(this);
        measure_ll_xueya_mailv = (LinearLayout) findViewById(R.id.measure_ll_xueya_mailv);
        measure_ll_xueyang = (LinearLayout) findViewById(R.id.measure_ll_xueyang);
        measure_ll_tiwen = (LinearLayout) findViewById(R.id.measure_ll_tiwen);
        TextView measure_jianhuyi_tv_title = (TextView) findViewById(R.id.main_all_tv_title);
        n = getIntent().getIntExtra("from", 1);
        // 1代表bp血压，2代表bo血氧，3代表体温
        if (n == 1) {
            stableBoButton.setEnabled(false);
            measureTempButton.setEnabled(false);
            stableBoButton.setVisibility(View.INVISIBLE);
            measureTempButton.setVisibility(View.INVISIBLE);
            measure_ll_xueyang.setVisibility(View.GONE);
            measure_ll_tiwen.setVisibility(View.GONE);
            measure_jianhuyi_tv_title.setText("【血压测量】");
        } else if (n == 2) {
            measureBpButton.setEnabled(false);
            measureTempButton.setEnabled(false);
            measureBpButton.setVisibility(View.INVISIBLE);
            measureTempButton.setVisibility(View.INVISIBLE);
            measure_ll_xueya_mailv.setVisibility(View.GONE);
            measure_ll_tiwen.setVisibility(View.GONE);
            tv_showBoData.setVisibility(View.VISIBLE);
            measure_jianhuyi_tv_title.setText("【血氧测量】");
            needrunning_showtext = true;
            Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    while (needrunning_showtext) {
                        if (is_showtextShan) {
                            String s = tv_showBoData.getText().toString()
                                    .trim();
                            if (!TextUtils.isEmpty(s) && !"--".equals(s)) {
                                handler.obtainMessage(what_setbotext_gone)
                                        .sendToTarget();
                                try {
                                    Thread.sleep(800);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                handler.obtainMessage(what_setbotext_visible)
                                        .sendToTarget();
                                try {
                                    Thread.sleep(800);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                handler.obtainMessage(what_setbotext_visible)
                                        .sendToTarget();
                            }
                        }
                    }
                }
            });
            thread.start();
        } else if (n == 3) {
            measureBpButton.setEnabled(false);
            stableBoButton.setEnabled(false);
            measureBpButton.setVisibility(View.INVISIBLE);
            stableBoButton.setVisibility(View.INVISIBLE);
            measure_ll_xueya_mailv.setVisibility(View.GONE);
            measure_ll_xueyang.setVisibility(View.GONE);
            measure_jianhuyi_tv_title.setText("【体温测量】");
        }
    }

    // 是不是需要线程
    boolean needrunning_showtext = false;
    // 是不是需要闪烁
    boolean is_showtextShan = false;

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (task != null) {
            task.cancel();
        }
        if (timer != null) {
            timer.cancel();
        }
        if (n == 1 && beneCheckDevice != null) {
            byte[] cmd = new byte[]{(byte) 0xEE, 0x35, 0x11, 00, 00, 0X55,
                    (byte) 0XCC};
            measureBpButton.setText("血压测量");
            beneCheckDevice.WriteData(cmd, cmd.length, 2000);
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.find_device:
                if (connect_status.getText().toString().trim().equals(Contants.Bluetooth_connecting.BLUETOOTH_CONNECTSUCCESS)) {
                    ToastTool.showShort(context, Contants.Bluetooth_connecting.BLUETOOTH_CONNECTSUCCESS_NOTCONNECTAGAIN);
                    return;
                }
                beneCheckDevice = new BeneCheckDeviceHB(myManager, this,
                        ACTION_USB_PERMISSION, handler);
                break;
            case R.id.upload_button:
                try {
                    if ("xueya".equals(flag)) {
                        Intent data = new Intent();
                        String sbp = lowBpEditText.getText().toString();
                        String dbp = HighBpEditText.getText().toString();
                        String pulse = pulseEditText.getText().toString();
                        data.putExtra("dbp", dbp);
                        data.putExtra("sbp", sbp);
                        data.putExtra("pulse", pulse);
                        JianHuYiActivity.this.setResult(2, data);
                        finish();
                    } else {
                        upload();// 上传数据
                    }
                } catch (JSONException e) {
                    ToastTool.showLong(context, "网络错误!");
                    e.printStackTrace();
                }
                break;
            case R.id.start_stop_bp_btn:
                if (beneCheckDevice != null && beneCheckDevice.isConnected()) {
                    if (measureBpButton.getText().toString().equals("血压测量")) {
                        int n = 0;
                        byte[] cc = null;
                        if (btn_ertong.isChecked()) {
                            cc = new byte[]{(byte) 0xEE, 0x35, 0x10, 01, 00,
                                    0X55, (byte) 0XCC};
                        } else if (btn_chengren.isChecked()) {
                            n = 0;
                            cc = new byte[]{(byte) 0xEE, 0x35, 0x10, 00, 00,
                                    0X55, (byte) 0XCC};
                        } else if (btn_daby.isChecked()) {
                            n = 2;
                            cc = new byte[]{(byte) 0xEE, 0x35, 0x10, 02, 00,
                                    0X55, (byte) 0XCC};
                        }
                        beneCheckDevice.WriteData(cc, cc.length, 2000);
                        measureBpButton.setText("停止测量");
                    } else if (measureBpButton.getText().toString().equals("停止测量")) {
                        byte[] cmd = new byte[]{(byte) 0xEE, 0x35, 0x11, 00, 00,
                                0X55, (byte) 0XCC};
                        measureBpButton.setText("血压测量");
                        beneCheckDevice.WriteData(cmd, cmd.length, 2000);
                    }

                }
                break;
            case R.id.bo_stable_button:
                sendGetData();
            /*
			 * if (beneCheckDevice != null && beneCheckDevice.isConnected()) {
			 * if (stableBoButton.getText().toString().equals("血氧测量")) { byte[]
			 * cmd = new byte[] { (byte) 0xEE, 0x35, 0x20, 00, 00, 0X55, (byte)
			 * 0XCC }; beneCheckDevice.WriteData(cmd, cmd.length, 2000);
			 * stableBoButton.setText("停止测量"); isrun = true; } else if
			 * (stableBoButton.getText().toString().equals("停止测量")) {
			 * stableBoButton.setText("血氧测量"); byte[] cmd = new byte[] { 0x21 };
			 * beneCheckDevice.WriteData(cmd, cmd.length, 2000); isrun = false;
			 * } }
			 */
                break;
        }

    }

    boolean isrun = true;

    @Override
    protected void onResume() {
        super.onResume();
        // * if (!beneCheckDevice.isConnected()) {
        // }*/
        TextView tv_service_state = (TextView) findViewById(R.id.tv_server_state);
        if (MyApplication.isConnected()) {
            tv_service_state.setTextColor(R.color.color_service_connected);
            tv_service_state.setText("已连接");
        } else {
            tv_service_state.setTextColor(R.color.color_service_noconnected);
            tv_service_state.setText("未连接");
        }
        // 如果是血氧就，实时的显示血氧值。
        sendGetData();
    }

    TimerTask task;
    Timer timer;

    public void sendGetData() {
        beneCheckDevice = new BeneCheckDeviceHB(myManager, this,
                ACTION_USB_PERMISSION, handler);
        if (n == 1) {
            return;
        }
        if (task != null) {
            task.cancel();
        }
        if (timer != null) {
            timer.cancel();
        }
        // 从本地sdcard卡获取更新文件，写入监护仪
        task = new TimerTask() {
            @Override
            public void run() {
                if (beneCheckDevice != null && beneCheckDevice.isConnected()) {
                    // 查询电量
                    byte[] cmd = new byte[]{(byte) 0xEE, 0x35, 0x20, 00, 00,
                            0X55, (byte) 0XCC};
                    beneCheckDevice.WriteData(cmd, cmd.length, 2000);
                    System.out.println("发送了一次命令,获取血氧值，2秒钟获取一次");
                }
            }
        };
        timer = new Timer();
        timer.schedule(task, 0, 2000);
    }

    final int what_setbotext_gone = 34;
    final int what_setbotext_visible = 98;
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case BeneCheckDeviceHB.WHAT_RECEIVE_TIMEOUT:
                    Toast.makeText(context, "usb接收命令数据超时，请检查设备是否故障！", Toast.LENGTH_SHORT).show();
                    break;
                case what_setbotext_gone:
                    tv_showBoData.setVisibility(View.GONE);
                    break;
                case what_setbotext_visible:
                    tv_showBoData.setVisibility(View.VISIBLE);
                    break;
                case UPLOAD_RESULT_OK:
                    ToastTool.showLong(context, "上传数据成功！");
                    if (lowBpEditText.getText().toString().length() > 0) {
                        setImageView(pulseImageView, 0);
                        setImageView(lBpImageView, 0);
                        setImageView(hBpImageView, 0);
                    } else if (boEditText.getText().toString().length() > 0) {
                        setImageView(boImageView, 0);
                    }
                    break;
                case UPLOAD_RESULT_ERROR:
                    ToastTool.showLong(context, "上传数据失败！");
                    if (lowBpEditText.getText().toString().length() > 0) {
                        setImageView(pulseImageView, 200);
                        setImageView(lBpImageView, 200);
                        setImageView(hBpImageView, 200);
                    } else if (boEditText.getText().toString().length() > 0) {
                        setImageView(boImageView, 200);
                    }
                    break;
                case 2:
                    Toast.makeText(JianHuYiActivity.this, "设备已断开！",
                            Toast.LENGTH_SHORT).show();
                    connect_status.setText("设备已断开");
                    connect_status.setTextColor(Color.RED);
                    break;
                case 4:
                    Toast.makeText(JianHuYiActivity.this, "设备打开失败！",
                            Toast.LENGTH_SHORT).show();
                    connect_status.setText(Contants.Bluetooth_connecting.BLUETOOTH_CONNECTFAILED);
                    connect_status.setTextColor(Color.RED);
                    break;
                case 5:
                    Toast.makeText(JianHuYiActivity.this, "设备打开成功！",
                            Toast.LENGTH_SHORT).show();
                    connect_status.setText(Contants.Bluetooth_connecting.BLUETOOTH_CONNECTSUCCESS);
                    connect_status.setTextColor(Color.BLUE);
                    break;
                case READER_DATA:
                    String data = (String) msg.obj;
                    data = data.replaceAll(" ", "");
                    int n = com.szosen.device.jianhuyi.Tools.getFrom(data);
                    switch (n) {
                        case 10:
                            if (data.length() == 14) {
                                String status = com.szosen.device.jianhuyi.Tools
                                        .getStatus(data);
                                //
                                // if (status.equals("其他错误")) {
                                // // 如果有其他错误，发送测量命令
                                // if (beneCheckDevice != null
                                // && beneCheckDevice.isConnected()) {
                                // int nn = 0;
                                // byte[] cc = null;
                                // if (btn_ertong.isChecked()) {
                                // cc = new byte[] { (byte) 0xEE, 0x35, 0x10,
                                // 01, 00, 0X55, (byte) 0XCC };
                                // } else if (btn_chengren.isChecked()) {
                                // nn = 0;
                                // cc = new byte[] { (byte) 0xEE, 0x35, 0x10,
                                // 00, 00, 0X55, (byte) 0XCC };
                                // } else if (btn_daby.isChecked()) {
                                // nn = 2;
                                // cc = new byte[] { (byte) 0xEE, 0x35, 0x10,
                                // 02, 00, 0X55, (byte) 0XCC };
                                // }
                                // beneCheckDevice.WriteData(cc, cc.length, 2000);
                                // }
                                // }
                                // 使用定时器，5秒钟后没有返回的数据，就断开设备。
                                // 提示重新连接设备，再发送命令
                                // else if("正在测试"){
                                //
                                // }
                                if (status.equals("正在测试") || status.equals("其他错误")) {
                                } else {
                                    ToastTool.showShortClear(context, status);
                                }
                            } else if (data.length() == 28) {
                                String[] result = com.szosen.device.jianhuyi.Tools
                                        .getBlood(data);
                                // 收到血压数据，显示按钮：血压测量。发送停止命令
                                HighBpEditText.setText(result[0]);
                                lowBpEditText.setText(result[1]);
                                pulseEditText.setText(result[3]);
                                measureBpButton.setText("血压测量");
                                // 停止测量的
                                byte[] cmd = new byte[]{0x21};
                                beneCheckDevice.WriteData(cmd, cmd.length, 2000);
                                results = "舒张压：" + result[0] + "   收缩压：" + result[1]
                                        + "   脉率:" + result[3];
                                uploadButton.setEnabled(true);
                                // measureBpButton.setText("开始血压测量");
                            }
                            break;
                        case 20:
                            if (data.indexOf("EE") == 0 && data.length() == 14) {
                                String[] result = com.szosen.device.jianhuyi.Tools
                                        .getXueyang(data);
                                if (result[0] != null && result[0].length() >= 1) {
                                    try {
                                        Integer showData = Integer.valueOf(result[0]);
                                        if (showData > 0 && showData <= 100) {
                                            // 实时显示血氧值
                                            tv_showBoData.setText(result[0]);
                                            is_showtextShan = true;
                                        } else {
                                            tv_showBoData.setText("--");
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        boEditText.setText(result[0]);
                                        tv_showBoData.setText("出错");
                                    }
                                    uploadButton.setEnabled(true);
                                    results = "血氧值：" + result[0];
                                }
                            }
                            break;
                        case 30:
                            if (data.indexOf("EE") == 0 && data.length() == 14) {
                                String result = com.szosen.device.jianhuyi.Tools
                                        .getWendu(data);
                                tempEditText.setText(result);
                                uploadButton.setEnabled(true);
                            }
                            break;
                        default:
                            break;
                    }
                    break;
                case 0:
                    Toast.makeText(context, "提交成功", 0).show();
                    if (lowBpEditText.getText().toString().length() > 0) {
                        setImageView(pulseImageView, 200);
                        setImageView(lBpImageView, 200);
                        setImageView(hBpImageView, 200);
                    } else if (boEditText.getText().toString().length() > 0) {
                        setImageView(boImageView, 200);
                    }
                    break;
                case -1:
                case -2:
                    if (lowBpEditText.getText().toString().length() > 0) {
                        setImageView(pulseImageView, 0);
                        setImageView(lBpImageView, 0);
                        setImageView(hBpImageView, 0);
                    } else if (boEditText.getText().toString().length() > 0) {
                        setImageView(boImageView, 0);
                    }
                    Toast.makeText(context, msg + "", 0).show();
                    break;

                case BluetoothService.MESSAGE_STATE_CHANGE:
                    switch (msg.arg1) {
                        case BluetoothService.STATE_CONNECTED: // 已连接
                            Toast.makeText(getApplicationContext(),
                                    "Connect successful", Toast.LENGTH_SHORT).show();

                            if (results.length() > 0) {
                                mService.sendMessage(results, "GBK");
                            }
                            Toast.makeText(getApplicationContext(), "打印完成",
                                    Toast.LENGTH_SHORT).show();
                            // btnClose.setEnabled(true);
                            // btnSend.setEnabled(true);
                            break;
                        case BluetoothService.STATE_CONNECTING: // 正在连接
                            Log.d("蓝牙调试", "正在连接.....");
                            break;
                        case BluetoothService.STATE_LISTEN: // 监听连接的到来
                        case BluetoothService.STATE_NONE:
                            Log.d("蓝牙调试", "等待连接.....");
                            break;
                    }
                    break;

                case BluetoothService.MESSAGE_UNABLE_CONNECT: // 无法连接设备
                    Toast.makeText(getApplicationContext(),
                            "Unable to connect device", Toast.LENGTH_SHORT).show();
                    break;

                default:
                    break;
            }
        }

        ;
    };
    private String flag = "";

    private static void setImageView(ImageView imageview, int status) {
        // if (status == WebService.OK)
        // imageview.setImageResource(R.drawable.light_greed);
        // else if (status == -1)
        // imageview.setImageResource(R.drawable.light_blue);
        // else
        // imageview.setImageResource(R.drawable.light_red);
    }

    private static void offlineSetImageView(ImageView imageview, int status) {
        // if (status == WebService.OK)
        // imageview.setImageResource(R.drawable.light_yellow);
        // else if (status == -1)
        // imageview.setImageResource(R.drawable.light_blue);
        // else
        // imageview.setImageResource(R.drawable.light_red);
    }

    @Override
    protected void onDestroy() {
        if (beneCheckDevice != null && beneCheckDevice.isConnected()) {
            beneCheckDevice.CloseDevice();
        }
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
        if (task != null) {
            task.cancel();
        }
        is_showtextShan = false;
        needrunning_showtext = false;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_ENABLE_BT: // 请求打开蓝牙
                if (resultCode == Activity.RESULT_OK) { // 蓝牙已经打开
                    Toast.makeText(this, "Bluetooth open successful",
                            Toast.LENGTH_LONG).show();
                } else { // 用户不允许打开蓝牙
                    finish();
                }
                break;
            case REQUEST_CONNECT_DEVICE: // 请求连接某一蓝牙设备
                if (resultCode == Activity.RESULT_OK) { // 已点击搜索列表中的某个设备项
                    // String address = data.getExtras().getString(
                    // DeviceListActivity.EXTRA_DEVICE_ADDRESS); // 获取列表项中设备的mac地址
                    // con_dev = mService.getDevByMac(address);
                    // mService.connect(con_dev);
                }
                break;
        }
    }
}
